import javax.jcr.Repository
import org.apache.jackrabbit.rmi.client.ClientRepositoryFactory
import javax.jcr.SimpleCredentials
import helpers.JcrHelper
import javax.jcr.Session
import javax.jcr.PropertyType
/**
 * Created by IntelliJ IDEA.
 * User: christian
 * Date: Jan 9, 2009
 * Time: 5:51:53 AM
 * To change this template use File | Settings | File Templates.
 */

public class JcrHelperTest extends GroovyTestCase {
    static Session session
    static {
        Repository repository
        ClientRepositoryFactory factory = new ClientRepositoryFactory();
            repository = factory.getRepository('rmi://localhost:1099/jackrabbit')
        session = repository.login(
                 new SimpleCredentials('admin', 'admin'.toCharArray()))
    }

    void testPropertyCreation(){
        JcrHelper helper = new JcrHelper(session)
        try {
            helper.withNode("/") { rootNode ->
                rootNode.addNode("test123", 'nt:unstructured')
            }
            session.save()
            def root = session.getRootNode()
            assertTrue(root.hasNode("test123"))
            //simple prop, create, default type
            helper.createProperty("/test123/one", PropertyType.STRING )
            def node  = root.getNode("test123")
            assertTrue(node.hasProperty("one"))
            assertTrue(node.getProperty("one").type==PropertyType.STRING)

            //simple prop, update, default type
            helper.saveProperty("/test123/one", false, PropertyType.BOOLEAN)
            session.save()
            assertTrue(node.hasProperty("one"))
            assertTrue(node.getProperty("one").type==PropertyType.BOOLEAN)
            assertFalse(node.getProperty("one").value.boolean)

            //simple prop, create, non-default type
            helper.createProperty("/test123/two", PropertyType.LONG )
            assertTrue(node.hasProperty("two"))
            assertTrue(node.getProperty("two").type==PropertyType.LONG)


            //simple prop, create, array args
            helper.saveProperty("/test123/three", ["this is my val", "and this not"])
            assertTrue(node.hasProperty("three"))
            assertTrue(node.getProperty("three").value.string=="this is my val")

            //mvp, create, simple arg
            helper.saveProperty("/test123/four", ["this is my val", "and this not"], PropertyType.STRING, true)
            assertTrue(node.hasProperty("four"))
            assertTrue(node.getProperty("four").values[0].string=="this is my val")

            //mvp, update, array arg

            helper.saveProperty("/test123/five", "this is my val", PropertyType.STRING, true)
            assertTrue(node.hasProperty("five"))
            assertTrue(node.getProperty("five").values[0].string=="this is my val")

        }finally {
            try { session.rootNode.getNode("test123").remove()}catch(Exception e){}
            session.save()

        }
    }
}